//
//  EMRegisterViewController.m
//  LoginAndReg
//
//  Created by fortsven on 14/12/23.
//  Copyright (c) 2014年 hoperun. All rights reserved.
//

#import "EMRegisterViewController.h"
#import "EMInputTextField.h"
#import "UINavigationController+EMNavi.h"
#import "EMRegistReqBody.h"
#import "EMRegistRespBody.h"
#import "MBProgressHUD.h"
#import "AppDelegate.h"
@interface EMRegisterViewController ()
{
    EMInputTextField *phoneNumTF;
    EMInputTextField *captchaTF;
    EMInputTextField *password1TF;
    EMInputTextField *password2TF;
    NSString *inputText;
    AppDelegate *app;
}
@end

@implementation EMRegisterViewController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.view.backgroundColor = RGB(242, 242, 242);
    
    [self.navigationController setNavigationMode:EMNavigationModeDefault andSubMode:0];
    self.navigationItem.title = @"用户注册";
    
    UIButton *button =[UIButton buttonWithType:UIButtonTypeCustom];
    button.frame=CGRectMake(0, 0, 30, 32);
    [button setImage:[UIImage imageNamed:@"button_导航_返回_白"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem =[[UIBarButtonItem alloc]initWithCustomView:button];
}
- (void)backAction: (UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    app = (AppDelegate *)[UIApplication sharedApplication].delegate;
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    scrollView.contentSize = CGSizeMake(W(320), H(800));
    
    int height = 25;
    phoneNumTF = [[EMInputTextField alloc] initWithFrame:CGRectMake(W(10), H(height), W(300), H(35))];
    phoneNumTF.placeholderText = @"请输入您的手机号";
    [phoneNumTF setTextFieldMode:EMInputTextFieldModeText];
    phoneNumTF.inputTextField.delegate = self;
    [scrollView addSubview:phoneNumTF];
    
    height += 30 + 15;
    captchaTF = [[EMInputTextField alloc] initWithFrame:CGRectMake(W(10), H(height), W(160), H(35))];
    captchaTF.placeholderText = @"请输入手机验证码";
    [captchaTF setTextFieldMode:EMInputTextFieldModeText];
    captchaTF.inputTextField.delegate = self;
    [scrollView addSubview:captchaTF];
    UIButton *getCaptchaBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    getCaptchaBtn.frame = CGRectMake(W(180), H(height), W(130), H(35));
    getCaptchaBtn.backgroundColor = RGB(54, 126, 255);
    getCaptchaBtn.layer.cornerRadius = 5;
    getCaptchaBtn.titleLabel.font = [UIFont boldSystemFontOfSize:14];
    [getCaptchaBtn setTitle:@"点击获取验证码" forState:UIControlStateNormal];
    [getCaptchaBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [getCaptchaBtn addTarget:self action:@selector(getCaptchaAction:) forControlEvents:UIControlEventTouchUpInside];
    [scrollView addSubview:getCaptchaBtn];
    
    height += 30 + 15;
    password1TF = [[EMInputTextField alloc] initWithFrame:CGRectMake(W(10), H(height), W(300), H(35))];
    password1TF.placeholderText = @"请输入您的密码";
    [password1TF setTextFieldMode:EMInputTextFieldModeText];
    password1TF.inputTextField.delegate = self;
    [scrollView addSubview:password1TF];
    
    height += 30 + 15;
    password2TF = [[EMInputTextField alloc] initWithFrame:CGRectMake(W(10), H(height), W(300), H(35))];
    password2TF.placeholderText = @"请再次输入密码";
    [password2TF setTextFieldMode:EMInputTextFieldModeText];
    password2TF.inputTextField.delegate = self;
    [scrollView addSubview:password2TF];
    
    
    height += 30 + 30;
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn.frame = CGRectMake(W(10), H(height), W(300), H(40));
    btn.backgroundColor = RGB(112, 179, 81);
    btn.layer.cornerRadius = 5;
    btn.titleLabel.font = [UIFont boldSystemFontOfSize:20];
    [btn setTitle:@"注册" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(registerAction:) forControlEvents:UIControlEventTouchUpInside];
    [scrollView addSubview:btn];
    
    [self.view addSubview:scrollView];
    
    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBackground:)];
    [scrollView addGestureRecognizer:tapGR];

}

#pragma mark - 关闭键盘
- (void)tapBackground:(UIGestureRecognizer *)recog
{
    [self closeKeyboard];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self closeKeyboard];
    return NO;
}

- (void)closeKeyboard
{
    [phoneNumTF.inputTextField resignFirstResponder];
    [captchaTF.inputTextField resignFirstResponder];
    [password1TF.inputTextField resignFirstResponder];
    [password2TF.inputTextField resignFirstResponder];
}

#pragma mark - 点击事件
- (void)getCaptchaAction:(UIButton *)sender
{
    NSLog(@"%@", phoneNumTF.inputTextField.text);
    [self showAlert:@"［获取验证码］正在开发，敬请期待"];
}
- (void)registerAction:(UIButton *)sender
{
    [MBProgressHUD  showHUDAddedTo:self.navigationController.view animated:YES];
    EMRegistReqBody *reqBody = [[EMRegistReqBody alloc] init];
    reqBody.JUDGEMETHOD = @"USER-REGIST";
    reqBody.USERID = @"13212341254";
    reqBody.PASSWORD = @"123456";
    reqBody.YZM = @"8888";
    HessianFormDataRequest *request = [[HessianFormDataRequest alloc] init];
    request.postData = reqBody.keyValues;
    [request startRequestWithURL:baseURL andCompletionBlcok:^(NSDictionary *respInfo) {
        EMRegistRespBody *respBody = [EMRegistRespBody objectWithKeyValues:respInfo];
        [MBProgressHUD hideHUDForView:self.navigationController.view animated:YES];
        NSLog(@"%@", respInfo);
        if ([respBody.ERRORCODE isEqualToString: @"0000"]) {
            [app showAlert:@"登录成功，敬请期待！"];
        } else {
            [app showTip:respInfo[@"ERRORDESTRIPTION"]];
        }
        
    } andFailedBlock:^{
        
        [app showTip:@"无法连接服务器、请稍后再试"];
    }];

    
    
    NSLog(@"%@,%@,%@,%@", phoneNumTF.inputTextField.text, captchaTF.inputTextField.text, password1TF.inputTextField.text, password2TF.inputTextField.text);
}
- (void)showAlert:(NSString *)content
{
    EMAttention *attention = [[EMAttention alloc] initWithTitle:@"提示" contentText:content leftButtonTitle:nil rightButtonTitle:@"确定"];
    [attention show];
}
@end
